package zuoye_4_14;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileCopy {
	static void copyFile(Path sourcePath, Path destinationPath) {
        try {
            long fileSize = Files.size(sourcePath);
            long copiedBytes = 0;
            byte[] buffer = new byte[1024];
            InputStream in = Files.newInputStream(sourcePath);
            OutputStream out = Files.newOutputStream(destinationPath);
            int bytesRead;
            while ((bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
                copiedBytes += bytesRead;
                double progress = (double) copiedBytes / fileSize * 100;
                System.out.printf("%s文件已复制%.2f%%\n", sourcePath.getFileName(), progress);
            }
            out.flush();
            in.close();
            out.close();
            System.out.printf("%s复制完成！\n", sourcePath.getFileName());
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
}
